package com.proposition.commons.exam.util;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.proposition.commons.exam.exception.ExamException;
import com.proposition.commons.exam.result.Result;
import com.proposition.commons.exam.result.ResultEnum;
import com.proposition.commons.exam.result.ResultUtil;
import com.proposition.commons.model.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.subject.Subject;

/**
 * @author: 陈玉林
 * @modifiedBy:
 * @date: Create In 14:52 2017/11/2
 * @description:
 */
public class ExamUtil {

    /**
     * @author: 陈玉林
     * @date: 2017/12/1 10:34
     * @description: 根据分类名称获取分类id
     */
    public static Integer getCategory(String substring) {
        Integer categoryId = 0;
        if (substring.equals("问答题-腧穴")) {
            categoryId = 1;
        } else if (substring.equals("问答题-内科")) {
            categoryId = 2;
        } else if (substring.equals("问答题-伤科")) {
            categoryId = 3;
        } else if (substring.equals("问答题-妇科")) {
            categoryId = 4;
        } else if (substring.equals("问答题-儿科")) {
            categoryId = 5;
        } else if (substring.equals("问答题-触诊")) {
            categoryId = 6;
        } else if (substring.equals("穴位定位-儿科穴位")) {
            categoryId = 7;
        } else if (substring.equals("穴位定位-成人穴位")) {
            categoryId = 8;
        } else if (substring.equals("单项手法-儿科手法")) {
            categoryId = 9;
        } else if (substring.equals("单项手法-成人手法")) {
            categoryId = 10;
        } else if (substring.equals("单项手法-触诊诊断")) {
            categoryId = 11;
        } else if (substring.equals("成套手法-内科")) {
            categoryId = 12;
        } else if (substring.equals("成套手法-伤科")) {
            categoryId = 13;
        } else if (substring.equals("成套手法-妇科")) {
            categoryId = 14;
        } else if (substring.equals("成套手法-儿科")) {
            categoryId = 15;
        }
        return categoryId;
    }

    /**
     * @author: 陈玉林
     * @date: 2017/12/4 16:50
     * @description: 验证试题导入大纲代码
     */
    public static ExamException volidateOutlineCode(String outlineCode,String examNum) {
        if (outlineCode == null || outlineCode.length()==0) {
            return new ExamException(examNum, ResultEnum.EMPTY_OUTLINE_CODE_ESSENTIAL);
        }
        if (!outlineCode.contains(".")) {
            return new ExamException(examNum, ResultEnum.ILLEGAL_OUTLINE_CODE);
        }
        String[] outlineCodeArr = outlineCode.split("\\.");
        if (outlineCodeArr.length != 5) {
            return new ExamException(examNum, ResultEnum.ILLEGAL_OUTLINE_CODE);
        } else if (!outlineCodeArr[0].equals("310") && !outlineCodeArr[0].equals("320")) {
            return new ExamException(examNum, ResultEnum.ILLEGAL_OUTLINE_CODE_EXAM_SYLLABUS);
        } else if (!outlineCodeArr[1].equals("11") && !outlineCodeArr[1].equals("12") && !outlineCodeArr[1].equals("13") && !outlineCodeArr[1].equals("14") && !outlineCodeArr[1].equals("21") && !outlineCodeArr[1].equals("22") && !outlineCodeArr[1].equals("31") && !outlineCodeArr[1].equals("32") && !outlineCodeArr[1].equals("33") && !outlineCodeArr[1].equals("34") && !outlineCodeArr[1].equals("35") && !outlineCodeArr[1].equals("41")) {
            return new ExamException(examNum, ResultEnum.ILLEGAL_OUTLINE_CODE_SUBJECT_CATEGORY);
        } else if (!outlineCodeArr[2].matches("^\\d+$") || outlineCodeArr[2].length()>2) {
            return new ExamException(examNum, ResultEnum.ILLEGAL_OUTLINE_CODE_UNIT);
        }else if (!outlineCodeArr[3].matches("^\\d+$") || outlineCodeArr[2].length()>2) {
            return new ExamException(examNum, ResultEnum.ILLEGAL_OUTLINE_CODE_DETAIL_ITEM);
        }else if (!outlineCodeArr[4].matches("^\\d+$") || outlineCodeArr[2].length()>2) {
            return new ExamException(examNum, ResultEnum.ILLEGAL_OUTLINE_CODE_ESSENTIAL);
        }
        return null;
    }
    /**
     * @author: 陈玉林
     * @date: 2017/12/4 16:50
     * @description: 验证自主命题大纲代码
     */
    public static void volidateOutlineCode(String outlineCode) {
        if (!outlineCode.contains(".")) {
            throw new ExamException( ResultEnum.ILLEGAL_OUTLINE_CODE);
        }
        String[] outlineCodeArr = outlineCode.split("\\.");
        if (outlineCodeArr.length != 5) {
            throw new ExamException( ResultEnum.ILLEGAL_OUTLINE_CODE);
        } else if (!outlineCodeArr[0].equals("310") && !outlineCodeArr[0].equals("320")) {
            throw new ExamException( ResultEnum.ILLEGAL_OUTLINE_CODE_EXAM_SYLLABUS);
        } else if (!outlineCodeArr[1].equals("11") && !outlineCodeArr[1].equals("12") && !outlineCodeArr[1].equals("13") && !outlineCodeArr[1].equals("14") && !outlineCodeArr[1].equals("21") && !outlineCodeArr[1].equals("22") && !outlineCodeArr[1].equals("31") && !outlineCodeArr[1].equals("32") && !outlineCodeArr[1].equals("33") && !outlineCodeArr[1].equals("34") && !outlineCodeArr[1].equals("35") && !outlineCodeArr[1].equals("41")) {
            throw new ExamException( ResultEnum.ILLEGAL_OUTLINE_CODE_SUBJECT_CATEGORY);
        } else if (!outlineCodeArr[2].matches("^\\d+$")) {
            throw new ExamException( ResultEnum.ILLEGAL_OUTLINE_CODE_UNIT);
        }else if (!outlineCodeArr[3].matches("^\\d+$")) {
            throw new ExamException( ResultEnum.ILLEGAL_OUTLINE_CODE_DETAIL_ITEM);
        }else if (!outlineCodeArr[4].matches("^\\d+$")) {
            throw new ExamException( ResultEnum.ILLEGAL_OUTLINE_CODE_ESSENTIAL);
        }
    }

    /**
     *@author: 陈玉林
     *@date: 2017/12/5 9:26
     *@description: 创建拼接试题id的分类字符串
     */
    public static String getCategoryStr(Integer categoryId) {
        if (categoryId == null) {
            return "01";
        }
        if (categoryId < 10) {
            return "0" + categoryId;
        }else {
            return "" + categoryId;
        }
    }

    /**
     *@author: 陈玉林
     *@date: 2017/12/20 11:15
     *@description: 保证试题参数已经设置
     */
    public static void ensureExamParamIsNotNull() {
        ExamParamWendaAcupoint examParamWendaAcupoint = new ExamParamWendaAcupoint().selectOne(new EntityWrapper());
        if (examParamWendaAcupoint==null) {
            examParamWendaAcupoint = new ExamParamWendaAcupoint();
            examParamWendaAcupoint.setMaxLocationScore(10.0f);
            examParamWendaAcupoint.setMaxMayorCureScore(10.0f);
            examParamWendaAcupoint.setMaxMeridianScore(10.0f);
            examParamWendaAcupoint.setMaxTotalScore(50.0f);
            examParamWendaAcupoint.insert();
        }
        ExamParamWendaFSENC examParamWendaFSENC = new ExamParamWendaFSENC().selectOne(new EntityWrapper());
        if (examParamWendaFSENC==null) {
            examParamWendaFSENC = new ExamParamWendaFSENC();
            examParamWendaFSENC.setMaxAnswerScore(10.0f);
            examParamWendaFSENC.setMaxTotalScore(50.0f);
            examParamWendaFSENC.insert();
        }
        ExamParamAcupointLocationAdult examParamAcupointLocationAdult = new ExamParamAcupointLocationAdult().selectOne(new EntityWrapper());
        if (examParamAcupointLocationAdult == null) {
            examParamAcupointLocationAdult = new ExamParamAcupointLocationAdult();
            examParamAcupointLocationAdult.setMaxPartScore(10.0f);
            examParamAcupointLocationAdult.setMaxMeridianScore(10.0f);
            examParamAcupointLocationAdult.setMaxLocationScore(10.0f);
            examParamAcupointLocationAdult.setMaxTotalScore(50.0f);
            examParamAcupointLocationAdult.insert();

        }
        ExamParamAcupointLocationPediatric examParamAcupointLocationPediatric = new ExamParamAcupointLocationPediatric().selectOne(new EntityWrapper());
        if (examParamAcupointLocationPediatric == null) {
            examParamAcupointLocationPediatric = new ExamParamAcupointLocationPediatric();
            examParamAcupointLocationPediatric.setMaxLocationScore(10.0f);
            examParamAcupointLocationPediatric.setMaxPartDetailScore(10.0f);
            examParamAcupointLocationPediatric.setMaxPartScore(10.0f);
            examParamAcupointLocationPediatric.setMaxTotalScore(50.0f);
            examParamAcupointLocationPediatric.insert();
        }
        ExamParamSinglePloyCEC examParamSinglePloyCEC = new ExamParamSinglePloyCEC().selectOne(new EntityWrapper());
        if (examParamSinglePloyCEC == null) {
            examParamSinglePloyCEC = new ExamParamSinglePloyCEC();
            examParamSinglePloyCEC.setMaxAnswerScore(10.0f);
            examParamSinglePloyCEC.setMaxTotalScore(50.0f);

            examParamSinglePloyCEC.insert();
        }
        ExamParamCompletePloyFSEN examParamCompletePloyFSEN = new ExamParamCompletePloyFSEN().selectOne(new EntityWrapper());
        if (examParamCompletePloyFSEN ==null) {
            examParamCompletePloyFSEN = new ExamParamCompletePloyFSEN();
            examParamCompletePloyFSEN.setMaxAnswerScore(10.0f);
            examParamCompletePloyFSEN.setMaxTotalScore(50.0f);
            examParamCompletePloyFSEN.insert();
        }
    }
    /**
     * @Author: 陈玉林
     * @Date: 2017/8/11 12:29
     * @Description: 验证权限
     */
    public static void volidateAuthorization(String permission) {
        Subject currentUser = SecurityUtils.getSubject();
        //权限不足
        if (!currentUser.isPermitted(permission)) {
            throw new AuthorizationException();
        }
    }
    /**
     *@author: 陈玉林 
     *@date: 2018/4/18 10:02 
     *@description: 验证权限
     */
    public static boolean validateAuth(String permission) {
        Subject currentUser = SecurityUtils.getSubject();
        //权限不足
        if (!currentUser.isPermitted(permission)) {
            return false;
        }
        return true;
    }
    public static Result volidateAuthBeforeIntoPage(String permission) {
        try {
            ExamUtil.volidateAuthorization(permission);
        } catch (AuthorizationException e) {
            e.printStackTrace();
            return new Result(ResultEnum.ILLEGAL_AUTHORITY, null);
        }
        return ResultUtil.sucess();
    }

    /**
     * 验证单项手法五大类
     * @param name 分类名称
     * @param examNum 试题题号
     * @return
     */
    public static Integer volidateSinglePloyFiveCategory(String name, String examNum) {
        Integer fiveCategoryId = 0;
        if (name==null || name == "") {
            return fiveCategoryId;
        }
        ExamSinglePloyFiveCategory examSinglePloyFiveCategory = new ExamSinglePloyFiveCategory();
        examSinglePloyFiveCategory=examSinglePloyFiveCategory.selectOne(new EntityWrapper().eq("name", name));
        if (examSinglePloyFiveCategory == null) {
            return null;
        }
        fiveCategoryId = examSinglePloyFiveCategory.getId();
        System.out.println(fiveCategoryId);
        return fiveCategoryId;
    }

    /**
     *@author: 陈玉林 
     *@date: 2018/4/16 14:13 
     *@description: 验证单项手法-儿科手法子类
     */
    public static Integer volidateSinglePloyPediatricSubclassId(String name, String examNum) {
        Integer singlePloyPediatricSubclassId = 0;
        if (name==null || name == "") {
            return singlePloyPediatricSubclassId;
        }
        ExamCategorySinglePloyPediatricSubclass examCategorySinglePloyPediatricSubclass = new ExamCategorySinglePloyPediatricSubclass();
        examCategorySinglePloyPediatricSubclass=examCategorySinglePloyPediatricSubclass.selectOne(new EntityWrapper().eq("name", name));
        if (examCategorySinglePloyPediatricSubclass == null) {
            return null;
        }
        singlePloyPediatricSubclassId = examCategorySinglePloyPediatricSubclass.getId();
        return singlePloyPediatricSubclassId;
    }

    public static Object[] setAnwerTenAnswer(ExamAnswer10 exam, ExamAnswer10Original examOriginal, String[] answer, Float[] answerScore) {
        Object[] examArr = new Object[2];
        for (int i = 0; i < answer.length; i++) {
            switch (i + 1) {
                case 1:
                    exam.setAnswer1(answer[i]);
                    examOriginal.setAnswer1(answer[i]);
                    break;
                case 2:
                    exam.setAnswer2(answer[i]);
                    examOriginal.setAnswer2(answer[i]);
                    break;
                case 3:
                    exam.setAnswer3(answer[i]);
                    examOriginal.setAnswer3(answer[i]);
                    break;
                case 4:
                    exam.setAnswer4(answer[i]);
                    examOriginal.setAnswer4(answer[i]);
                    break;
                case 5:
                    exam.setAnswer5(answer[i]);
                    examOriginal.setAnswer5(answer[i]);
                    break;
                case 6:
                    exam.setAnswer6(answer[i]);
                    examOriginal.setAnswer6(answer[i]);
                    break;
                case 7:
                    exam.setAnswer7(answer[i]);
                    examOriginal.setAnswer7(answer[i]);
                    break;
                case 8:
                    exam.setAnswer8(answer[i]);
                    examOriginal.setAnswer8(answer[i]);
                    break;
                case 9:
                    exam.setAnswer9(answer[i]);
                    examOriginal.setAnswer9(answer[i]);
                    break;
                case 10:
                    exam.setAnswer10(answer[i]);
                    examOriginal.setAnswer10(answer[i]);
                    break;

                default:
                    break;
            }
        }
        for (int i = 0; i < answerScore.length; i++) {
            switch (i + 1) {
                case 1:
                    exam.setAnswer1Score(answerScore[i]);
                    examOriginal.setAnswer1Score(answerScore[i]);
                    break;
                case 2:
                    exam.setAnswer2Score(answerScore[i]);
                    examOriginal.setAnswer2Score(answerScore[i]);
                    break;
                case 3:
                    exam.setAnswer3Score(answerScore[i]);
                    examOriginal.setAnswer3Score(answerScore[i]);
                    break;
                case 4:
                    exam.setAnswer4Score(answerScore[i]);
                    examOriginal.setAnswer4Score(answerScore[i]);
                    break;
                case 5:
                    exam.setAnswer5Score(answerScore[i]);
                    examOriginal.setAnswer5Score(answerScore[i]);
                    break;
                case 6:
                    exam.setAnswer6Score(answerScore[i]);
                    examOriginal.setAnswer6Score(answerScore[i]);
                    break;
                case 7:
                    exam.setAnswer7Score(answerScore[i]);
                    examOriginal.setAnswer7Score(answerScore[i]);
                    break;
                case 8:
                    exam.setAnswer8Score(answerScore[i]);
                    examOriginal.setAnswer8Score(answerScore[i]);
                    break;
                case 9:
                    exam.setAnswer9Score(answerScore[i]);
                    examOriginal.setAnswer9Score(answerScore[i]);
                    break;
                case 10:
                    exam.setAnswer10Score(answerScore[i]);
                    examOriginal.setAnswer10Score(answerScore[i]);
                    break;

                default:
                    break;
            }
        }
        examArr[0]=exam;
        examArr[1]=examOriginal;
        return examArr;
    }

    public static Object[] setAnswerXWDW(ExamAcupointAdult exam, ExamAcupointAdultOriginal examOriginal, String[] answer, Float[] answerScore) {
        Object[] objs = new Object[2];
        for (int i = 0; i < answer.length; i++) {
            switch (i + 1) {
                case 1:
                    exam.setLocation1Adult(answer[i]);
                    examOriginal.setLocation1Adult(answer[i]);
                    break;
                case 2:
                    exam.setLocation2Adult(answer[i]);
                    examOriginal.setLocation2Adult(answer[i]);
                    break;
                case 3:
                    exam.setLocation3Adult(answer[i]);
                    examOriginal.setLocation3Adult(answer[i]);
                    break;
                default:
                    break;
            }
        }
        for (int i = 0; i < answerScore.length; i++) {
            switch (i + 1) {
                case 1:
                    exam.setLocation1ScoreAdult(answerScore[i]);
                    examOriginal.setLocation1ScoreAdult(answerScore[i]);
                    break;
                case 2:
                    exam.setLocation2ScoreAdult(answerScore[i]);
                    examOriginal.setLocation2ScoreAdult(answerScore[i]);
                    break;
                case 3:
                    exam.setLocation3ScoreAdult(answerScore[i]);
                    examOriginal.setLocation3ScoreAdult(answerScore[i]);
                    break;
                default:
                    break;
            }
        }
        objs[0]=exam;
        objs[1]=examOriginal;
        return objs;
    }
}
